import os
import zipfile
import requests
from tqdm import tqdm


# Mod更新器配置
mod_directory = '.\\.minecraft\\versions\\1.20.4'
mod_zip_url = f'https://gitee.com/cyo57/mcmod/releases/download/latest/mcmod.zip'

# 下载Mod文件
def download_mods(url, local_file):
    response = requests.get(url, stream=True)
    total_size = int(response.headers.get('content-length', 0))
    with open(local_file, 'wb') as f:
        for data in tqdm(response.iter_content(1024), total=total_size / 1024, unit='KB', unit_scale=True):
            f.write(data)

# 解压Mod文件
def extract_mods(zip_file, dest_dir):
    with zipfile.ZipFile(zip_file, 'r') as zip_ref:
        zip_ref.extractall(dest_dir)

# 主函数
def main():
    print('正在下载Mod文件...')
    mods_zip = 'mods.zip'
    download_mods(mod_zip_url, mods_zip)
    print('下载完成！正在解压Mod文件...')
    extract_mods(mods_zip, mod_directory)
    os.remove(mods_zip)
    print('Mod更新完成！')
    input('现在可关闭窗口')

if __name__ == '__main__':
    main()
